﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using ShopB2C.DBUtilities;
using ShopB2C.DBUtilities.SqlDbHelper;
using ShopB2C.Entity;
using ShopB2C.Interfaces;

namespace ShopB2C.SQLDataAccess {
    public class News : INews {

        #region Const Variables

        private const string _newsAppend = "News_append";
        private const string _newsUpdate = "News_Update";
        private const string _newsDelete = "News_Delete";
        private const string _newsSelect = "News_Select";
        private const string _newsSelectList = "News_SelectList";

        #endregion

        public News(string connectionString) {
            DbBase.ConnectionString = connectionString;
        }

        #region News

        public void NewsAppend(NewsEntity ne) {
            DbParams<SqlParameter> sqlParams = new DbParams<SqlParameter>();
            sqlParams.Add<String>("@Author", 20, ne.Author).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<String>("@Title", 50, ne.Title).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<String>("@Content", ne.Content).SqlDbType = SqlDbType.NText;
            sqlParams.Add<String>("@MetaKeywords", 500, ne.MetaKeywords).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<String>("@MetaDescription", 1000, ne.MetaDescription).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<Boolean>("@IsAllowReplay", ne.IsAllowReplay).SqlDbType = SqlDbType.Bit;
            sqlParams.Add<Int32>("@ClassifyId", ne.ClassifyId).SqlDbType = SqlDbType.Int;
            SqlDbProcedure.ExecuteNonQuery(_newsAppend, sqlParams.ToArray);
        }

        public void NewsUpdate(NewsEntity ne) {
            DbParams<SqlParameter> sqlParams = new DbParams<SqlParameter>();
            sqlParams.Add<Int32>("@NewsId", ne.NewsId).SqlDbType = SqlDbType.Int;
            sqlParams.Add<String>("@Author", 20, ne.Author).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<String>("@Title", 50, ne.Title).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<String>("@Content", ne.Content).SqlDbType = SqlDbType.NText;
            sqlParams.Add<String>("@MetaKeywords", 500, ne.MetaKeywords).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<String>("@MetaDescription", 1000, ne.MetaDescription).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<Boolean>("@IsAllowReplay", ne.IsAllowReplay).SqlDbType = SqlDbType.Bit;
            sqlParams.Add<Int32>("@ClassifyId", ne.ClassifyId).SqlDbType = SqlDbType.Int;
            SqlDbProcedure.ExecuteNonQuery(_newsUpdate, sqlParams.ToArray);
        }

        public void NewsDelete(string newsIds) {
            SqlDbProcedure.ExecuteNonQuery(_newsDelete,
                DbParams<SqlParameter>.CreateParameter<string>("@NewsIds", newsIds));
        }

        public NewsEntity GetNewsById(int newsId) {
            NewsEntity ne = null;
            SqlDataReader dr = SqlDbProcedure.ExecuteReader(_newsSelect,
                DbParams<SqlParameter>.CreateParameter<int>("@NewsId", newsId));
            if (dr.HasRows) {
                if (dr.Read()) {
                    ne = new NewsEntity() {
                        NewsId = (int)dr["NewsId"],
                        Author = dr["Author"].ToString(),
                        Title = dr["Title"].ToString(),
                        Content = dr["Content"].ToString(),
                        MetaKeywords = dr["MetaKeywords"].ToString(),
                        MetaDescription = dr["MetaDescription"].ToString(),
                        AddDate = (DateTime)dr["AddDate"],
                        IsAllowReplay = (bool)dr["IsAllowReplay"],
                        ClassifyId = (int)dr["ClassifyId"]
                    };
                }
            }
            dr.Close();
            dr.Dispose();
            return ne;
        }

        public List<NewsEntity> GetNewsList(int pageSize, int pageIndex, out int pageCount) {
            return this.GetNewsList(0, string.Empty, pageSize, pageIndex, out pageCount);
        }

        public List<NewsEntity> GetNewsList(int classifyId, int pageSize, int pageIndex, out int pageCount) {
            return this.GetNewsList(classifyId, string.Empty, pageSize, pageIndex, out pageCount);
        }

        public List<NewsEntity> GetNewsList(int classifyId, string key, int pageSize, int pageIndex, out int pageCount) {
            pageCount = 0;
            List<NewsEntity> listResult = listResult = new List<NewsEntity>();
            DbParams<SqlParameter> sqlParams = new DbParams<SqlParameter>();
            sqlParams.Add<Int32>("@PageCount", ParameterDirection.Output, 0).SqlDbType = SqlDbType.Int;
            sqlParams.Add<Int32>("@PageSize", pageSize).SqlDbType = SqlDbType.Int;
            sqlParams.Add<Int32>("@PageIndex", pageIndex).SqlDbType = SqlDbType.Int;
            sqlParams.Add<Int32>("@ClassifyId", classifyId).SqlDbType = SqlDbType.Int;
            sqlParams.Add<string>("@Key", key).SqlDbType = SqlDbType.NVarChar;
            SqlDataReader dr = SqlDbProcedure.ExecuteReader(_newsSelectList, sqlParams.ToArray);
            if (dr.HasRows) {
                while (dr.Read()) {
                    pageCount = (int)dr["PageCount"];
                    listResult.Add(new NewsEntity() {
                        NewsId = (int)dr["NewsId"],
                        Author = dr["Author"].ToString(),
                        Title = dr["Title"].ToString(),
                        Content = dr["Content"].ToString(),
                        MetaKeywords = dr["MetaKeywords"].ToString(),
                        MetaDescription = dr["MetaDescription"].ToString(),
                        AddDate = (DateTime)dr["AddDate"],
                        IsAllowReplay = (bool)dr["IsAllowReplay"],
                        ClassifyId = (int)dr["ClassifyId"],
                        ClassifyName = dr["ClassifyName"].ToString()
                    });
                }
            }
            dr.Close();
            dr.Dispose();
            return listResult;
        }

        #endregion

    }
}
